package la.foton.atividade.util.exception;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;

public class AjaxAwareAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint
{

   public AjaxAwareAuthenticationEntryPoint()
   {

   }

   public AjaxAwareAuthenticationEntryPoint(final String loginFormUrl)
   {
      super(loginFormUrl);
   }

   @Override
   public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException)
      throws IOException, ServletException
   {

      if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With")))
      {
         response.sendError(901, "Forbidden");
         // response.sendRedirect(super.getLoginFormUrl());
      }
      else
      {
         super.commence(request, response, authException);
      }
   }

}
